package com.itheima.bookcontroller;

import com.itheima.bookservice.BookService;
import com.itheima.domain.Book;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public abstract class BaseBookController {

    private BookService bookService = new BookService();
    public void star() {
        while (true){

            printView();

            userSelect();

        }

    }

    public abstract void printView();

    public void userSelect(){
        System.out.println("请输入您的选择：");

        Scanner sc = new Scanner(System.in);
        String next = sc.next();

        switch (next) {

            case "1" :
                addBook();
                break;
            case "2" :
                deleteBook();
                break;
            case "3" :
                updateBook();
                break;
            case "4" :
                getAllBooks();
                break;
            case "5" :
                System.out.println("欢迎下次使用");
                System.exit(0);
                break;
            default:
                System.out.println("您的选择有误，请重新选择");
                break;

        }
    }

    public void getAllBooks() {
        System.out.println("图书id\t\t图书名称\t\t发布时间\t\t图书价格");
        Book[] books = bookService.getAllBooks();
        for (int i = 0; i < books.length; i++) {
            Book bookArr = books[i];
            if(bookArr != null) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String format = simpleDateFormat.format(bookArr.getPublishDate());
                System.out.println(bookArr.getBookId() + "\t\t" + bookArr.getBookName() + "\t\t" + format + "\t\t" + bookArr.getPrice());
            }

        }
    }

    public void updateBook() {
        while(true) {
            System.out.println("请输入要修改的图书id：");
            Scanner sc = new Scanner(System.in);
            String bookId = sc.next();

            boolean result = bookService.isExitsBook(bookId);
            if(result) {
                System.out.println("请输入新的图书名称：");
                String bookName = sc.next();

                System.out.println("请输入新的图书发布时间：");
                String publishDate = sc.next();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date parse = null;
                try {
                    parse = simpleDateFormat.parse(publishDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                System.out.println("请输入新图书的价格：");
                String price = sc.next();
                double parseDouble = Double.parseDouble(price);

                Book book = new Book();
                book.setBookId(bookId);
                book.setBookName(bookName);
                book.setPublishDate(parse);
                book.setPrice(parseDouble);

                bookService.updateBook(book);
                System.out.println("修改图书成功");
                break;
            }else{
                System.out.println("您输入的图书id不存在，请重新输入");
            }
        }
    }

    public void deleteBook() {
        while(true){
            System.out.println("请输入要删除的图书id：");
            Scanner sc = new Scanner(System.in);
            String bookId = sc.next();

            boolean isExitsBook = bookService.isExitsBook(bookId);

            if(isExitsBook) {
                bookService.deleteBook(bookId);
                System.out.println("删除图书成功");
                break;
            }else{
                System.out.println("您输入的图书id不存在，请重新输入");
            }
        }
    }

    public void addBook() {

        while (true) {

            System.out.println("请输入要添加的图书id：");
            Scanner sc = new Scanner(System.in);
            String bookId = sc.next();

            boolean isExitsBook = bookService.isExitsBook(bookId);

            if(isExitsBook){
                System.out.println("您输入的id已存在，请重新输入");
            }else{
                System.out.println("请输入图书名称：");
                String bookName = sc.next();

                System.out.println("请输入图书发布时间：");
                String publishDate = sc.next();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date parse = null;
                try {
                    parse = simpleDateFormat.parse(publishDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                System.out.println("请输入图书价格：");
                String price = sc.next();
                double parseDouble = Double.parseDouble(price);

                Book book = new Book();
                book.setBookId(bookId);
                book.setBookName(bookName);
                book.setPublishDate(parse);
                book.setPrice(parseDouble);

                boolean addBook = bookService.addBook(book);
                if(addBook) {
                    System.out.println("添加图书成功");
                    break;
                }else{
                    System.out.println("添加图书失败");
                }

            }

        }

    }

}
